001    /* EVolve - an Extensible Software Visualization Framework
002     * Copyright (C) 2001-2002 Qin Wang
003     *
004     * This library is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU Library General Public
006     * License as published by the Free Software Foundation; either
007     * version 2 of the License, or (at your option) any later version.
008     *
009     * This library is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012     * Library General Public License for more details.
013     *
014     * You should have received a copy of the GNU Library General Public
015     * License along with this library; if not, write to the
016     * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
017     * Boston, MA 02111-1307, USA.
018     */
019    
020    /*
021     * EVolve is distributed at http://www.sable.mcgill.ca/EVolve/
022     */
023    
024    package EVolve.data;
025    
026    import EVolve.*;
027    import java.awt.*;
028    import java.util.*;
029    
030    public class ElementFilter {
031        private int[] linkIndex;
032        private HashMap[] color;
033        protected Color colorBlue = new Color(120, 160, 255);
034    
035        public ElementFilter(ElementDefinition elementDefinition, Selection[] selection) {
036            int count = 0;
037            for (int i = 0; i < elementDefinition.getFieldDefinition().length; i++) {
038                if (elementDefinition.getFieldDefinition()[i].getReference() != -1) {
039                    count++;
040                }
041            }
042            linkIndex = new int[count];
043            count = 0;
044            for (int i = 0; i < elementDefinition.getFieldDefinition().length; i++) {
045                if (elementDefinition.getFieldDefinition()[i].getReference() != -1) {
046                    linkIndex[count] = i;
047                    count++;
048                }
049            }
050    
051            color = new HashMap[linkIndex.length];
052            for (int i = 0; i < color.length; i++) {
053                //color[i] = new Color[Scene.getDataManager().getEntity()[elementDefinition.getFieldDefinition()[linkIndex[i]].getReference()].size()];
054                color[i] = new HashMap();
055                HashMap entities = Scene.getDataManager().getEntity()[elementDefinition.getFieldDefinition()[linkIndex[i]].getReference()];
056                Iterator it = entities.keySet().iterator();
057                while (it.hasNext()) {
058                    Long key = (Long)it.next();
059                    color[i].put(key,colorBlue);
060                    for (int k = 0; k < selection.length; k++) {
061                        boolean isSelected = true;
062    
063                        for (int l = 0; l < selection[k].getLink().length; l++) {
064                            if ((selection[k].getLink()[l].getSourceType() == elementDefinition.getType()) &&
065                                (selection[k].getLink()[l].getSourceIndex() == linkIndex[i])) {
066                                isSelected = false;
067                                for (int m = 0; m < selection[k].getSelected().length; m++) {
068                                    if (selection[k].getLink()[l].getTarget(key.longValue()) == selection[k].getSelected()[m].getId()) {
069                                        isSelected = true;
070                                        if ((color[i].get(key) == colorBlue) && (selection[k].getColor() != null)) {
071                                            color[i].put(key,selection[k].getColor());
072                                        }
073                                        break;
074                                    }
075                                }
076                                break;
077                            }
078                        }
079                        if ((!isSelected) && (selection[k].getColor() == null)) {
080                            color[i].put(key,null);
081                            break;
082                        }
083                    }
084                }
085    
086                /*for (int j = 0; j < color[i].length; j++) {
087                    color[i][j] = colorBlue;//Color.black;
088                    for (int k = 0; k < selection.length; k++) {
089                        boolean isSelected = true;
090                        for (int l = 0; l < selection[k].getLink().length; l++) {
091                            if ((selection[k].getLink()[l].getSourceType() == elementDefinition.getType()) && (selection[k].getLink()[l].getOriginMappedId() == linkIndex[i])) {
092                                isSelected = false;
093                                for (int m = 0; m < selection[k].getSelected().length; m++) {
094                                    if (selection[k].getLink()[l].getTarget(j) == selection[k].getSelected()[m].getId()) {
095                                        isSelected = true;
096                                        if ((color[i][j] == colorBlue) && (selection[k].getColor() != null)) {
097                                            color[i][j] = selection[k].getColor();
098                                        }
099                                        break;
100                                    }
101                                }
102                                break;
103                            }
104                        }
105                        if ((!isSelected) && (selection[k].getColor() == null)) {
106                            color[i][j] = null;
107                            break;
108                        }
109                    }
110                }*/
111            }
112        }
113    
114        public Color getColor(Element element) {
115            Color result = colorBlue;//Color.black;
116            for (int i = 0; i < linkIndex.length; i++) {
117                if (color[i].get(new Long(element.getField()[linkIndex[i]])) == null) {
118                    return null;
119                }
120                if (result == colorBlue/*Color.black*/) {
121                    result = (Color)color[i].get(new Long(element.getField()[linkIndex[i]]));
122                }
123            }
124            return result;
125        }
126    }